import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-07-04
 * Time: 14:26
 */
public class Test19 {
    public static List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> ret = new ArrayList<>();
        int m = matrix.length, n = matrix[0].length;
        int top = 0,bot = m-1,left = 0, right = n-1;
        //特殊判断
        if(matrix == null || m == 0) {
            return ret;
        }
        while(left <= right && top <= bot) {
            // 上
            for(int i = left; i <= right; i++) {
                ret.add(matrix[top][i]);
            }
            top++;
            //右
            for(int i = top; i <= bot; i++) {
                ret.add(matrix[i][right]);
            }
            right--;
            //下
            if(top <= bot) {
                for(int i = right; i >= left; i--) {
                    ret.add(matrix[bot][i]);
                }
                bot--;
            }
            //左
            if(left <= right) {
                for(int i = bot; i >= top; i--) {
                    ret.add(matrix[i][left]);
                }
                left++;
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] num = new int[m][n];
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                num[i][j] = sc.nextInt();
            }
        }
        List<Integer> ret = spiralOrder(num);
        System.out.println(ret);
    }
}
